// CodeContracts
// 
// Copyright (c) Microsoft Corporation
// 
// All rights reserved. 
// 
// MIT License
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#if INCLUDE_UNSAFE_ANALYSIS

// This file contains the implementation of weak bound analysis
using System;
using Generics = System.Collections.Generic;
using System.Text;
using Microsoft.Research.AbstractDomains.Numerical;
using Microsoft.Research.AbstractDomains;
using Microsoft.Research.DataStructures;
using Microsoft.Research.AbstractDomains.Expressions;
using System.Collections.Generic;

namespace Microsoft.Research.CodeAnalysis
{
  public static partial class AnalysisWrapper
  {
    // This class is just to provide bindings to type parameters
    public static partial class TypeBindings<Local, Parameter, Method, Field, Property, Type, Attribute, Assembly, ExternalExpression, Variable>
      where Variable : IEquatable<Variable>
      where ExternalExpression : IEquatable<ExternalExpression>
      where Type : IEquatable<Type>
    {
      /// <summary>
      /// The implementation of NumericalAnalysis specialized for intervals of Rationals
      /// </summary>
      internal class PentagonUnsafeCodeAnalysis : UnsafeCodeAnalysis
      {
        #region Constructor

        public PentagonUnsafeCodeAnalysis(
          string methodName,
          IMethodDriver<APC, Local, Parameter, Method, Field, Property, Type, Attribute, Assembly, ExternalExpression, Variable, ILogOptions> mdriver,
          List<Analyzers.Unsafe.UnsafeOptions> options,
          IOverallUnsafeStatistics overallStats
          )
          : base(methodName, mdriver, options, overallStats)
        {
        }
        #endregion

        /// <summary>
        /// Get the top element made by a top interval environment and a top weak upper bounds environment
        /// </summary>
        override internal protected INumericalAbstractDomain<BoxedExpression> InitialValue
        {
          get
          {
            IntervalEnvironment<BoxedExpression> intervalEnvironment = new IntervalEnvironment<BoxedExpression>(this.Decoder, this.Encoder);
            WeakUpperBounds<BoxedExpression> weakUpperBoundsEnvironment = new WeakUpperBounds<BoxedExpression>(this.Decoder, this.Encoder);

            return new Pentagons<BoxedExpression>(intervalEnvironment, weakUpperBoundsEnvironment, this.Decoder, this.Encoder);
          }
        }

        /// <summary>
        /// Assume the property <code>exp >= 0</code>
        /// </summary>
        protected override INumericalAbstractDomain<BoxedExpression> AssumeGreaterEqualThanZero(BoxedExpression exp,
          INumericalAbstractDomain<BoxedExpression> data)
        {
          var cast = (Pentagons<BoxedExpression>)data;

          var newLeft = cast.Left.TestTrueGeqZero(exp);
          var newRight = cast.Right; // We abstract them away

          return new Pentagons<BoxedExpression>(newLeft, newRight, this.Decoder, this.Encoder);
        }
      }
    }
  }
}

#endif